
const crypto = require('crypto');
const rp = require('request-promise');

module.exports = class extends think.Service {
  async sign(data) {
    const appId = '123';
    const appsecret = '';
    const nonce = this.randomString(4);
    const timeStamp = new Date().getTime();
    const sign = crypto.createHash('md5').update(`${appId}${appsecret}${data}${nonce}${timeStamp}`, 'utf8').digest('hex');
    return sign;
  }
  randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = $chars.length;
    var pwd = '';
    for (let i = 0; i < len; i++) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
  }
  timeFormate(date) {
    const str = date.substring(0, date.length() - 11);
    const arr = date.split('-');
    return [arr[0], str + arr[1]];
  }
  async pushOrder(data, goods) {
    console.log(data);
    console.log(goods);
    const products = [];
    for (const item of goods) {
      products.push({
        name: item.title,
        amount: item.num
      });
    }
    const orderDetail = {
      order_id: data.order_no,
      order_price: data.actual_price,
      consumer_phone: data.give_mobile,
      message: data.remark,
      signature: '',
      receiver: {
        name: data.consignee,
        gender: data.gender,
        phone: data.mobile
      },
      products: products,
      delivery: {
        address: {
          province: data.province,
          city: data.city,
          district: data.address,
          etc: data.area
        },
        time: {
          from: this.timeFormate(data.reserve_time)[0],
          to: this.timeFormate(data.reserve_time)[1]
        }
      }
    };
    const orderInfo = Object.assign(orderDetail, {signature: this.sign(orderDetail)});
    const sendOptions = {
      method: 'POST',
      url: 'https://api.hua1000.com/aaaa/order/push',
      body: orderInfo
    };
    try {
      const requestResult = await rp(sendOptions);
      console.log(requestResult);
      return requestResult;
    } catch (err) {
      return err;
    }
  }
  async orderOpt(id, type, str) {
    const data = {
      'order_id': id,
      'status': type,
      'reason': str
    };
    const orderInfo = Object.assign(data, {signature: this.sign(data)});
    const sendOptions = {
      method: 'POST',
      url: 'https://api.hua1000.com/aaaa/order/cancel',
      body: orderInfo
    };
    try {
      const requestResult = await rp(sendOptions);
      console.log(requestResult);
      return requestResult;
    } catch (err) {
      return err;
    }
  }
  async orderConfirm(id) {
    const data = {
      'order_id': id
    };
    const orderInfo = Object.assign(data, {signature: this.sign(data)});
    const sendOptions = {
      method: 'POST',
      url: 'https://api.hua1000.com/aaaa/order/finish',
      body: orderInfo
    };
    try {
      const requestResult = await rp(sendOptions);
      console.log(requestResult);
      return requestResult;
    } catch (err) {
      return err;
    }
  }
};
